<?php
class ModelProductManufacturer extends Model {
	public function addManufacturer($data) {
		$name = $this->db->escape($data['name']);
		$sql = "INSERT INTO " . DB_PREFIX . "manufacturer SET `name` = '" . $name;
		if($data['image']){
			$sql .= "', image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8'));;
		}
		if($data['authorize_image']){
			$sql .= "', authorize_image = '" . $this->db->escape(html_entity_decode($data['authorize_image'], ENT_QUOTES, 'UTF-8'));
		}
		$sql .= "', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "'";
		$this->db->query($sql);
      	$manufacturer_id = $this->db->getLastId();
      	
		if (isset($data['manufacturer_description'])) {
			foreach ($data['manufacturer_description'] as $language_id => $value) {
				$sql_desc = "INSERT INTO " . DB_PREFIX . "manufacturer_description SET manufacturer_id = '" . (int)$manufacturer_id . 
						"', language_id = '" . (int)$language_id . "', display_name = '";
				if($value['display_name']){
					$sql_desc .= $this->db->escape($value['display_name']);
				}else{
					$sql_desc .= $name;
				}
				$sql_desc .= "', authorize_desc = '" . $this->db->escape($value['authorize_desc']) . 
						"', manufacturer_desc = '" . $this->db->escape($value['manufacturer_desc']) . "'";
				$this->db->query($sql_desc);
			}
		}
		$this->cache->delete('manufacturer');
	}
	
	public function editManufacturer($manufacturer_id, $data) {
		$name = $this->db->escape($data['name']);
		$sql = "UPDATE " . DB_PREFIX . "manufacturer SET `name` = '" . $name;
		if($data['image']){
			$sql .= "', image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "'";
		}else{
			$sql .= "', image = null";
		}
		if($data['authorize_image']){
			$sql .= ", authorize_image = '" . $this->db->escape(html_entity_decode($data['authorize_image'], ENT_QUOTES, 'UTF-8')) . "'";
		}else{
			$sql .= ", authorize_image = null";
		}
		$sql .= ", sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . 
			"' WHERE manufacturer_id = '" . (int)$manufacturer_id . "'";
		$this->db->query($sql);
		
      	$this->db->query("DELETE FROM " . DB_PREFIX . "manufacturer_description WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");
		if (isset($data['manufacturer_description'])) {
			foreach ($data['manufacturer_description'] as $language_id => $value) {
				$sql_desc = "INSERT INTO " . DB_PREFIX . "manufacturer_description SET manufacturer_id = '" . (int)$manufacturer_id . 
						"', language_id = '" . (int)$language_id . "', display_name = '";
				if($value['display_name']){
					$sql_desc .= $this->db->escape($value['display_name']);
				}else{
					$sql_desc .= $name;
				}
				$sql_desc .= "', authorize_desc = '" . $this->db->escape($value['authorize_desc']) . 
						"', manufacturer_desc = '" . $this->db->escape($value['manufacturer_desc']) . "'";
				$this->db->query($sql_desc);
			}
		}
		$this->cache->delete('manufacturer');
	}
	
	public function getManufacturer($manufacturer_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "manufacturer WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");
		return $query->row;
	}
	
	public function getManufacturerDescription($manufacturer_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "manufacturer_description WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");
		return $query->rows;
	}
	
	public function getManufacturers($data = array()) {
		$sql = "SELECT m.*, md.display_name FROM " . DB_PREFIX . "manufacturer m LEFT JOIN " . 
			DB_PREFIX . "manufacturer_description md ON(m.manufacturer_id = md.manufacturer_id AND md.language_id = '" . 
			(int)$this->config->get('config_language_id') . "')";
		
		if (isset($data['filter_display_name']) && $data['filter_display_name'] != "") {
			$sql .= " WHERE LCASE(md.display_name) LIKE '" . $this->db->escape($data['filter_display_name']) . "%'";
		}
		
		$sort_data = array(
			'md.display_name',
			'm.sort_order'
		);	
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY md.display_name";	
		}
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}					
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}				
		$query = $this->db->query($sql);
		return $query->rows;
	}

	public function getTotalManufacturers($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "manufacturer m LEFT JOIN " . 
			DB_PREFIX . "manufacturer_description md ON(m.manufacturer_id = md.manufacturer_id AND md.language_id = '" . 
			(int)$this->config->get('config_language_id') . "')";
		
		if (isset($data['filter_display_name']) && $data['filter_display_name'] != "") {
			$sql .= " WHERE LCASE(md.display_name) LIKE '" . $this->db->escape($data['filter_display_name']) . "%'";
		}
		
      	$query = $this->db->query($sql);
		return $query->row['total'];
	}	
}
?>